package com.geeksaga.search;

import java.io.Reader;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.LowerCaseFilter;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.analysis.StopFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.util.Version;

/**
 * @see org.apache.lucene.analysis.standard.StandardAnalyzer
 * 
 * @author newbie
 * @since 0.1
 */
@SuppressWarnings("rawtypes")
public class KoreanAnalyzer extends Analyzer {
	public static final int DEFAULT_MAX_TOKEN_LENGTH = 255;
	public static final Set/* <String> */STOP_WORDS_SET = StopAnalyzer.ENGLISH_STOP_WORDS_SET;

	private Set stopSet;
	private boolean enableStopPositionIncrements;
	private boolean useDefaultStopPositionIncrements;
	private int maxTokenLength = DEFAULT_MAX_TOKEN_LENGTH;

	public KoreanAnalyzer(Version matchVersion) {
		this(matchVersion, STOP_WORDS_SET);
	}

	public KoreanAnalyzer(Version matchVersion, Set stopWords) {
		stopSet = stopWords;
		init(matchVersion);
	}

	/**
	 * @see org.apache.lucene.analysis.standard.StandardAnalyzer.init()
	 * 
	 * @param matchVersion
	 */
	@SuppressWarnings("deprecation")
	private final void init(Version matchVersion) {
		if (matchVersion.onOrAfter(Version.LUCENE_29)) {
			enableStopPositionIncrements = true;
		} else {
			useDefaultStopPositionIncrements = true;
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	public TokenStream tokenStream(String fieldName, Reader reader) {
		// WhitespaceTokenizer tokenStream = new WhitespaceTokenizer(reader);
		StandardTokenizer tokenStream = new StandardTokenizer(reader);
		tokenStream.setMaxTokenLength(maxTokenLength);

		TokenStream result = new KoreanFilter(tokenStream);
		result = new LowerCaseFilter(result);

		if (useDefaultStopPositionIncrements) {
			result = new StopFilter(result, stopSet);
		} else {
			result = new StopFilter(enableStopPositionIncrements, result, stopSet);
		}

		return result;
	}
}